﻿using System;
using System.Collections.Generic;
using System.Text;

using System.Web.UI;

namespace Duson.Controls.Pager
{
    /// <summary>
    /// 下拉框跳转方式
    /// </summary>
    public class SkipType_DropDownList : SkipTypeAbstract
    {
        public override void CreateControls(HtmlTextWriter writer)
        {
            string lstPageID = MyPager.UniqueID + "_" + ConstParams.SelectPageID_Suffix;

            writer.AddAttribute(HtmlTextWriterAttribute.Name, lstPageID);
            writer.AddAttribute(HtmlTextWriterAttribute.Id, lstPageID);
            writer.AddAttribute(HtmlTextWriterAttribute.Onchange, MyPager.PagingMethod == PagingMethod.Url ? "UrlPage('" + ConstParams.Url_Params + "', document.getElementById('" + lstPageID + "').value);" : "__doPostBack('" + MyPager.ClientID + "',document.getElementById('" + lstPageID + "').value)");
            writer.RenderBeginTag(HtmlTextWriterTag.Select);

            if (MyPager.TotalPage > 30) // 列出一部分页码
            {
                if (MyPager.PageIndex <= 15)
                {
                    listPageIndexes(writer, 1, 15);
                    addMoreListItem(writer, 16);
                    listPageIndexes(writer, MyPager.TotalPage - 4, MyPager.TotalPage);
                }
                else if (MyPager.PageIndex >= MyPager.TotalPage - 14)
                {
                    listPageIndexes(writer, 1, 5);
                    addMoreListItem(writer, MyPager.TotalPage - 15);
                    listPageIndexes(writer, MyPager.TotalPage - 14, MyPager.TotalPage);
                }
                else
                {
                    listPageIndexes(writer, 1, 5);
                    addMoreListItem(writer, MyPager.PageIndex - 6);
                    listPageIndexes(writer, MyPager.PageIndex - 5, MyPager.PageIndex + 5);
                    addMoreListItem(writer, MyPager.PageIndex + 6);
                    listPageIndexes(writer, MyPager.TotalPage - 4, MyPager.TotalPage);
                }
            }
            else // 列出全部页码
                listPageIndexes(writer, 1, MyPager.TotalPage);
        }

        #region 私有函数
        /// <summary>
        /// 添加其它页码的链接
        /// </summary>
        /// <param name="writer"></param>
        /// <param name="pageIndex"></param>
        private void addMoreListItem(HtmlTextWriter writer, int pageIndex)
        {
            writer.Write("<option value=\"");
            writer.Write(pageIndex);
            writer.Write("\">......</option>");
        }

        /// <summary>
        /// 列出页码
        /// </summary>
        /// <param name="writer"></param>
        /// <param name="startIndex"></param>
        /// <param name="endIndex"></param>
        private void listPageIndexes(HtmlTextWriter writer, int startIndex, int endIndex)
        {
            for (int i = startIndex; i <= endIndex; i++)
            {
                writer.Write("<option value=\"");
                writer.Write(i);
                writer.Write("\"");
                if (i == MyPager.PageIndex)
                    writer.Write(" selected=\"true\"");
                writer.Write(">");
                writer.Write(i);
                writer.Write("</option>");
            }
        }
        #endregion

    }
}
